﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Xqsys.DAL
{
    public class Users
    {
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="user">参数</param>
        /// <returns>bool成功与否</returns>
        public bool AddUser(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userName",user.Username),
                new SqlParameter("@password",user.Password),
                new SqlParameter("@email",user.Email),
                new SqlParameter ("@regTime",user.RegTime),
                new SqlParameter("@role",user.Role)
            };
            return COM.SqlHelper.ExecuteNonQuery("sp_AddUsers", param);
        }
        /// <summary>
        /// 检测用户是否存在
        /// </summary>
        /// <param name="userName"></param>
        /// <returns>bool 成功与否</returns>
        public bool ExistUserName(string userName)
        {
            SqlParameter[] param=new SqlParameter[]
            {
                new SqlParameter("@userName",userName)
            };
            if ((int)COM.SqlHelper.ExecuteScalar("sp_ExitsUserName",param)==0)
                return false;
            else 
                return true;
        }
        /// <summary>
        /// 用户资料提交
        /// </summary>
        /// <param name="user"></param>
        /// <returns>bool 成功与否</returns>
        public bool UpdateUser(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userID",user.UserID),
                new SqlParameter("@email",user.Email),
                new SqlParameter("@realName",user.RealName),
                new SqlParameter("@sex",user.Sex),
                new SqlParameter("@jopID",user.JopID),
                new SqlParameter("@phone",user.Phone),
                new SqlParameter("@photo",user.Photo),
                new SqlParameter("@address",user.Address),
                new SqlParameter("@introduction",user.Introduction),
            };
            return COM.SqlHelper.ExecuteNonQuery("sp_UpdateUser", param);
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="user">参数用户名</param>
        /// <returns>用户所有资料</returns>
        public DataSet Login(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userName",user.Username),
            };
            return COM.SqlHelper.ExecuteQuery("sp_SelectUser",param);
        }
        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="user">参数用户id</param>
        /// <returns>用户所有资料</returns>
        public DataSet SelectUserID(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userID",user.UserID)
            };
            return COM.SqlHelper.ExecuteQuery("sp_SelectUserID", param);
        }
        /// <summary>
        /// 注销用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns>bool</returns>
        public bool DeleteUser(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userID",user.UserID)
            };
            if (COM.SqlHelper.ExecuteScalar("sp_DeleteUser", param) == null)
                return true;
            else
                return false;          
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="user"></param>
        /// <returns>bool</returns>
        public bool ChangerPwd(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userID",user.UserID)
            };
            return COM.SqlHelper.ExecuteNonQuery("sp_ChangerPwd", param);
        }
        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns>用户表</returns>
        public DataSet GetAllList()
        {
            return COM.SqlHelper.ExecuteQuery("sp_GetAllList");
        }
        /// <summary>
        /// 管理用户更新
        /// </summary>
        /// <param name="user"></param>
        /// <returns>bool 成功与否</returns>
        public bool UpDating(MODEL.Users user)
        {
            SqlParameter[] param = new SqlParameter[]
            {
                new SqlParameter("@userID",user.UserID),
                new SqlParameter("@role",user.Role),
                new SqlParameter("@mark",user.Mark),
                new SqlParameter("@appraise",user.Appraise)
            };
            return COM.SqlHelper.ExecuteNonQuery("sp_UpDating", param);
        }
    }
}
